package maintain

import (
	"iot-base/common/webapi"
	"os"
	"path/filepath"

	"github.com/gin-gonic/gin"
)

func initVersionAPI(r *gin.RouterGroup) {
	r.GET("/ver/", func(c *gin.Context) {
		dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
		path := dir + "/ver"
		var f *os.File
		var err error
		_, err = os.Lstat(path)
		isExist := !os.IsNotExist(err)

		if isExist {
			f, err = os.Open(path)
		} else {
			f, err = os.Create(path)
		}
		if f != nil {
			defer f.Close()
			buf := make([]byte, 100)
			n, _ := f.Read(buf)
			if n > 0 {
				webapi.SendResult(string(buf[0:n]), nil, c)
				return
			}

		}
		webapi.SendResult("1.001", nil, c)

	})
}
